package net.oschina.app.base;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import net.oschina.app.protocol.Constant;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Shinelon on 2017/1/15.
 */

public abstract class BaseListAdapter<T> extends RecyclerView.Adapter {
    public List<T> mList = new ArrayList<>();

    public void setData(List<T> list){
        mList = list;
        notifyDataSetChanged();
    }

    public void addData(T data){
        mList.add(data);
        notifyDataSetChanged();
    }

    public void addDatas(List<T> list){
        mList.addAll(list);
        notifyDataSetChanged();
    }

    public void clearData(){
        mList.clear();
    }

    @Override
    public int getItemCount() {
        return mList==null?0:mList.size();
    }

    // 跳转到对应的Activity
    public void setOnItemClick(View itemView, final Context context, final Class<? extends BaseActivity> clss, final String ...values) {
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, clss);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                if (values != null && values.length != 0) {
                    intent.putExtra(Constant.WV, values);
                }
                context.startActivity(intent);
            }
        });
    }
}
